package com.sunny;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Cyclic{

    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2 = lock.newCondition();
    private Condition condition3 = lock.newCondition();
    private volatile int state = 1;

    private class A implements Runnable{
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    if (1 != state)
                        condition1.await();
                    System.out.println("A");
                    state = 2;
                    condition2.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
                try {
//                    System.out.println("A休眠");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private class B implements Runnable{
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    if(2 != state)
                        condition2.await();
                    System.out.println("B");
                    state = 3;
                    condition3.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();
                }
                try {
//                    System.out.println("B休眠");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    private class C implements Runnable{
        @Override
        public void run() {
            while (true) {
                lock.lock();
                try {
                    if(3 != state)
                        condition3.await();
                    System.out.println("C");
                    state = 1;
                    condition1.signal();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    lock.unlock();
                }
                try {
//                    System.out.println("C休眠");
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {

        Cyclic cyclic = new Cyclic();
        ExecutorService tp = Executors.newFixedThreadPool(3);
        Cyclic.A a = cyclic.new A();
        Cyclic.B b = cyclic.new B();
        Cyclic.C c = cyclic.new C();
        tp.execute(a);
        tp.execute(b);
        tp.execute(c);


    }

}
